package com.project.controller.thymeleaf;

import com.github.pagehelper.PageInfo;
import com.project.bean.StudentBean;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/thy")
public class StudentController {
    @Autowired
    private IStudentService service;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "pageNO",defaultValue = "1") int pageNO, Model model){
        PageInfo<StudentBean> pageInfo = service.cutAll(pageNO);
        model.addAttribute("pageObj",pageInfo);
        return "/html/student/index.html";
    }

    @RequestMapping("/add")
    public String add(StudentBean student){
        service.addStudent(student);
        return "redirect:/thy/findAll";
    }

    @RequestMapping("/del")
    public String del(int id){
        service.del(id);
        return "redirect:/thy/findAll";
    }

    @RequestMapping("/findById")
    public String findById(int id, Model model){
        StudentBean s = service.findById(id);
        model.addAttribute("student",s);
        return "/html/student/update.html";
    }

    @RequestMapping("/update")
    public String update(int id, int grade){
        service.update(id,grade);
        return "redirect:/thy/findAll";
    }

}
